package com.bluefoxyu;

import com.bluefoxyu.game.Direction;
import com.bluefoxyu.proxy.ProxyFactory;
import com.bluefoxyu.service.SnakeGameService;

import java.util.Scanner;

public class SnakeGameConsumer {
    public static void main(String[] args) {
        SnakeGameService service = ProxyFactory.lookup(SnakeGameService.class);

        Scanner scanner = new Scanner(System.in);
        // 初始化，获取该用户的sessionId
        String sessionId = service.startGame();

        System.out.println("使用 w/a/s/d 控制方向，输入 q 结束游戏");

        boolean isRunning = true;
        while (isRunning) {
            String input = scanner.nextLine();
            Direction dir = null;
            switch (input) {
                case "w":
                    dir = Direction.UP;
                    break;
                case "s":
                    dir = Direction.DOWN;
                    break;
                case "a":
                    dir = Direction.LEFT;
                    break;
                case "d":
                    dir = Direction.RIGHT;
                    break;
                case "q":
                    isRunning = false;
                    // 游戏结束时清除 session
                    service.clearGameMap(sessionId);
                    System.out.println("游戏结束");
                    break;
                default:
                    System.out.println("无效输入，请输入 w/a/s/d 或 q 结束游戏");
                    break;
            }

            if (dir != null) {
                // 一个用户一个会话sessionId
                service.move(sessionId, dir);
            }
        }

        scanner.close();
    }
}